//
//  HomeSleepDataCell.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/21.
//

#import "HomeSleepDataCell.h"
#import "HomeSleepDataView.h"

@interface HomeSleepDataCell()
@property (nonatomic,strong) HomeSleepDataView * dataView;
@property (nonatomic,strong) UILabel * beforeLabel;
@property (nonatomic,strong) UILabel * beforeTypeLabel;
@property (nonatomic,strong) UILabel * beforeInfoLabel;
@property (nonatomic,strong) UILabel * sleepLabel;
@property (nonatomic,strong) UILabel * sleepTypeLabel;
@property (nonatomic,strong) UILabel * sleepInfoLabel;

@end

@implementation HomeSleepDataCell

-(void)setSubViewLayout{
    [super setSubViewLayout];
    
    
    
    [self addSubview:self.dataView];
    [_dataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgview.mas_left).offset(19 * FMScaleX);
        make.right.equalTo(self.bgview.mas_right).offset(-18 * FMScaleX);
        make.top.equalTo(self.iconView.mas_bottom).offset(19 * FMScaleX);
        make.height.mas_offset(110 * FMScaleX);
    }];
    
    [self addSubview:self.beforeLabel];
    [_beforeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dataView);
        make.top.equalTo(self.iconView.mas_bottom).offset(24 * FMScaleX);
        make.height.mas_offset(22 * FMScaleX);
    }];
    [self addSubview:self.beforeTypeLabel];
    [_beforeTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.beforeLabel.mas_right).offset(10 * FMScaleX);
        make.height.mas_offset(20 * FMScaleX);
        make.centerY.equalTo(self.beforeLabel);
    }];
    [self addSubview:self.beforeInfoLabel];
    [_beforeInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dataView);
        make.height.mas_offset(14 * FMScaleX);
        make.top.equalTo(self.beforeLabel.mas_bottom).offset(3 * FMScaleX);
        make.width.mas_offset(195 * FMScaleX);
    }];
    
    [self addSubview:self.sleepLabel];
    [_sleepLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dataView);
        make.top.equalTo(self.beforeInfoLabel.mas_bottom).offset(13 * FMScaleX);
        make.height.mas_offset(22 * FMScaleX);
    }];
    [self addSubview:self.sleepTypeLabel];
    [_sleepTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sleepLabel.mas_right).offset(10 * FMScaleX);
        make.height.mas_offset(20 * FMScaleX);
        make.centerY.equalTo(self.sleepLabel);
    }];
    [self addSubview:self.sleepInfoLabel];
    [_sleepInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dataView);
        make.height.mas_offset(14 * FMScaleX);
        make.top.equalTo(self.sleepLabel.mas_bottom).offset(3 * FMScaleX);
        make.width.mas_offset(100 * FMScaleX);
    }];
    
//    [self.sleepInfoLabel setCornerRadius:3 * FMScaleX rectCorner:UIRectCornerTopRight | UIRectCornerBottomRight];
    [self.beforeInfoLabel setCornerRadius:3 * FMScaleX rectCorner:UIRectCornerTopRight | UIRectCornerBottomRight];

}


-(void)setInfoModel:(HomeSleepStressModel *)infoModel{
    
    self.beforeTypeLabel.text = [self textForValue:infoModel.beforeSleepScore];
    self.sleepTypeLabel.text = [self textForValue:infoModel.afterSleepScore];

    UIColor *beforColor = [self colorForValue:infoModel.beforeSleepScore];
    UIColor *sleepColor = [self colorForValue:infoModel.afterSleepScore];

    self.beforeTypeLabel.backgroundColor = [beforColor colorWithAlphaComponent:0.3];
    self.sleepTypeLabel.backgroundColor = [sleepColor colorWithAlphaComponent:0.3];
    
    self.beforeInfoLabel.backgroundColor = beforColor;
    self.sleepInfoLabel.backgroundColor = sleepColor;
    
    
    CGFloat width = (WIDTH - 70 * FMScaleX) /100;
    [self.beforeInfoLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(width * infoModel.beforeSleepScore);
//        make.left.equalTo(self.dataView);
//        make.height.mas_offset(14 * FMScaleX);
//        make.top.equalTo(self.beforeLabel.mas_bottom).offset(3 * FMScaleX);
    }];
    [self.sleepInfoLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(width * infoModel.afterSleepScore);
//        make.left.equalTo(self.dataView);
//        make.height.mas_offset(14 * FMScaleX);
//        make.top.equalTo(self.sleepLabel.mas_bottom).offset(3 * FMScaleX);
    }];
       
}

- (UIColor *)colorForValue:(CGFloat)value {
    UIColor *color;
    if (value >= 0 && value < 25) {
        color = RDRGB(0x799BE8);
    } else if (value >= 25 && value < 50) {
        color = RDRGB(0x6FBBA4);
    } else if (value >= 50 && value < 75) {
        color = RDRGB(0xF3B66E);
    } else if (value >= 75 && value <= 100) {
        color = RDRGB(0xF295A5);
    } else {
        color = [UIColor clearColor];
    }
    return color;
   
}

- (NSString *)textForValue:(CGFloat)value {
    if (value >= 0 && value < 25) {
        return @" 放松 ";
    } else if (value >= 25 && value < 50) {
        return @" 正常 ";
    } else if (value >= 50 && value < 75) {
        return @" 中等 ";
    } else if (value >= 75 && value <= 100) {
        return @" 偏高 ";
    }
    return @" 未知 ";
}


-(HomeSleepDataView *)dataView{
    
    if (!_dataView) {
        _dataView = [[HomeSleepDataView alloc]init];
    }
    return _dataView;
}

-(UILabel *)beforeLabel{
    
    if (!_beforeLabel) {
        _beforeLabel = [[UILabel alloc]init];
        _beforeLabel.font = Regular(13);
        _beforeLabel.text = @"睡前压力状态";
        _beforeLabel.textColor = RDRGB(0x555555);
    }
    return _beforeLabel;
}
-(UILabel *)beforeTypeLabel{
    
    if (!_beforeTypeLabel) {
        _beforeTypeLabel = [[UILabel alloc]init];
        _beforeTypeLabel.font = Medium(12);
        _beforeTypeLabel.textColor = RDRGB(0xF3B66E);
        _beforeTypeLabel.text = @" 可改善 ";
        _beforeTypeLabel.backgroundColor = RDRGBA(0xF3B66E, 0.2);
        [_beforeTypeLabel setCornerRadius:4 * FMScaleX];
    }
    return _beforeTypeLabel;
}
-(UILabel *)beforeInfoLabel{
    
    if (!_beforeInfoLabel) {
        _beforeInfoLabel = [[UILabel alloc]init];
        _beforeInfoLabel.backgroundColor = RDRGB(0xF3B66E);
    }
    return _beforeInfoLabel;
}

-(UILabel *)sleepLabel{
    
    if (!_sleepLabel) {
        _sleepLabel = [[UILabel alloc]init];
        _sleepLabel.font = Regular(13);
        _sleepLabel.text = @"睡眠压力状态";
        _sleepLabel.textColor = RDRGB(0x555555);
    }
    return _sleepLabel;
}
-(UILabel *)sleepTypeLabel{
    
    if (!_sleepTypeLabel) {
        _sleepTypeLabel = [[UILabel alloc]init];
        _sleepTypeLabel.font = Medium(12);
        _sleepTypeLabel.textColor = RDRGB(0x6FBBA4);
        _sleepTypeLabel.text = @" 良好 ";
        _sleepTypeLabel.backgroundColor = RDRGBA(0x6FBBA4, 0.2);
        [_sleepTypeLabel setCornerRadius:4 * FMScaleX];
    }
    return _sleepTypeLabel;
}
-(UILabel *)sleepInfoLabel{
    
    if (!_sleepInfoLabel) {
        _sleepInfoLabel = [[UILabel alloc]init];
        _sleepInfoLabel.backgroundColor = RDRGB(0x6FBBA4);
    }
    return _sleepInfoLabel;
}
@end
